﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin1_Default2 : System.Web.UI.Page
{
    //Datacon dataconn = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["ID"] == null)
            {
                Response.Write("<script>alert('非法登录！');location='../admin.aspx'</script>");
            }
            else
            {
                //dataconn.bindinfostring(gvQueInfo, "select * from SingleProblem where c_id in(select c_id from Course where teacher_id='"+Session["ID"]+"')  order by ID DESC", "ID");
                using (DataClassesDataContext ds = new DataClassesDataContext())
                {
                    gvQueInfo.DataSource = from q in ds.SingleProblem
                                           join c in ds.Course on q.c_id equals c.c_id
                                           orderby q.ID descending
                                           select new {q.ID,c.c_name,q.Title };
                    gvQueInfo.DataBind();

                }
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            if (TextBox1.Text == "")
            {
                //dataconn.bindinfostring(gvQueInfo, "select * from SingleProblem where c_id in(select c_id from Course where teacher_id='" + Session["ID"] + "')  order by ID DESC", "ID");
                gvQueInfo.DataSource = from q in ds.SingleProblem
                                       join c in ds.Course on q.c_id equals c.c_id
                                       orderby q.ID descending
                                       select new { q.ID, c.c_name, q.Title };
                gvQueInfo.DataBind();
            }
            else
            {
                //dataconn.bind(gvQueInfo, "select * from SingleProblem where c_id in(select c_id from Course where teacher_id='" + Session["ID"] + "') and " + DropDownList1.SelectedValue + " Like'%" + TextBox1.Text + "%'");
                if (DropDownList1.Text == "课程")
                {
                    gvQueInfo.DataSource = from q in ds.SingleProblem
                                           join c in ds.Course on q.c_id equals c.c_id
                                           where c.c_name.Contains(TextBox1.Text.Trim())
                                           orderby q.ID descending
                                           select new { q.ID, c.c_name, q.Title };
                    gvQueInfo.DataBind();
                }
                else if (DropDownList1.Text == "题目")
                {
                    gvQueInfo.DataSource = from q in ds.SingleProblem
                                           join c in ds.Course on q.c_id equals c.c_id
                                           where q.Title.Contains(TextBox1.Text.Trim())
                                           orderby q.ID descending
                                           select new { q.ID, c.c_name, q.Title };
                    gvQueInfo.DataBind();
                }
            }
        }
    }
    protected void gvQueInfo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Session["Name"] = DropDownList1.SelectedValue;
        Response.Redirect("ChangeDanXuan.aspx");
    }
    protected void gvQueInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //dataconn.eccom("delete from SingleProblem where ID='" + gvQueInfo.DataKeys[e.RowIndex].Value + "'");
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            var rd = from q in ds.SingleProblem
                     where q.ID == Convert.ToInt32(gvQueInfo.DataKeys[e.RowIndex].Value)
                     select q;
            ds.SingleProblem.DeleteAllOnSubmit(rd);
            ds.SubmitChanges();
        }
        Response.Redirect("ManageDanXuan.aspx");
    }
    protected void gvQueInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            gvQueInfo.PageIndex = e.NewPageIndex;
            if (TextBox1.Text == "")
            {
                //dataconn.bindinfostring(gvQueInfo, "select * from SingleProblem where c_id in(select c_id from Course where teacher_id='" + Session["ID"] + "')  order by ID DESC", "ID");
                gvQueInfo.DataSource = from q in ds.SingleProblem
                                       join c in ds.Course on q.c_id equals c.c_id
                                       orderby q.ID descending
                                       select new { q.ID, c.c_name, q.Title };
                gvQueInfo.DataBind();
            }
            else
            {
                //dataconn.bind(gvQueInfo, "select * from SingleProblem where c_id in(select c_id from Course where teacher_id='" + Session["ID"] + "') and " + DropDownList1.SelectedValue + " Like'%" + TextBox1.Text + "%'");
                if (DropDownList1.Text == "课程")
                {
                    gvQueInfo.DataSource = from q in ds.SingleProblem
                                           join c in ds.Course on q.c_id equals c.c_id
                                           where c.c_name.Contains(TextBox1.Text.Trim())
                                           orderby q.ID descending
                                           select new { q.ID, c.c_name, q.Title };
                    gvQueInfo.DataBind();
                }
                else if (DropDownList1.Text == "题目")
                {
                    gvQueInfo.DataSource = from q in ds.SingleProblem
                                           join c in ds.Course on q.c_id equals c.c_id
                                           where q.Title.Contains(TextBox1.Text.Trim())
                                           orderby q.ID descending
                                           select new { q.ID, c.c_name, q.Title };
                    gvQueInfo.DataBind();
                }
            }
        }
    }
}